package com.smart.custom;

import com.fr.base.FRContext;
import com.fr.data.AbstractTableData;
import com.fr.file.DatasourceManager;
import com.fr.general.data.TableDataException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class demoQueryData extends AbstractTableData {

    private static final long serialVersionUID = 8965247661342925737L;

    // 字段名枚举
      enum FIELD_NAME {
        PROJECTS, YEARS, POERIODS
      }

      private String[] columnName;
      private int colNum = 0;
        private ArrayList<Object> rowData = null;



    public  demoQueryData(){
//        this.parameters = new Parameter[] { new Parameter("tablename")};

        columnName = new String[FIELD_NAME.values().length];
                int i = 0;
                 for (FIELD_NAME fieldName : FIELD_NAME.values()) {
                     columnName[i] = fieldName.toString();
                         i++;
                     }


    }
    @Override
    public int getColumnCount() throws TableDataException {
        System.out.println("getColumnCount");
        colNum=columnName.length;
        return columnName.length;
    }

    @Override
    public String getColumnName(int i) throws TableDataException {
        System.out.println("getColumnName"+i);
        return columnName[i];
    }

    @Override
    public int getRowCount() throws TableDataException {
        System.out.println("getRowCount");
        queryData();
        return rowData.size();
    }

    @Override
    public Object getValueAt(int i, int i1) {
        System.out.println("getValueAt--iI1"+i+i1);
        System.out.println("--"+colNum);
        if (i1 >=colNum ) {
            return null;
        }
        System.out.println("--"+((Object[]) rowData.get(i))[i1]);
        return ((Object[]) rowData.get(i))[i1];
    }

    private void queryData(){
        if(rowData!=null){
            return;

        }
        //String tableName = ((ParameterProvider) (parameters.get().toArray())[0]).getValue().toString();
        String tableName = parameters[0].getValue().toString();
        System.out.println(tableName);
        rowData = new ArrayList<Object>();
        String sql="select * from  FORM1_企业所得税 ";
        FRContext.getLogger().info("Query SQL of ParamTableDataDemo: \n" + sql);
        com.fr.data.impl.Connection conn = DatasourceManager.getInstance().getConnection("FRDemo");
        try {
            Connection con = conn.createConnection();
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
//            ResultSetMetaData rsmd=rs.getMetaData();
            Object[] objArray = null;
            while(rs.next()){
                objArray = new Object[columnName.length];
                for (int i = 0; i < colNum; i++) {
                    objArray[i] = rs.getObject(i + 1);
                    System.out.println("query+"+objArray[i]);
                }
                // 在valueList中加入这一行数据
                rowData.add(objArray);
            }
            // 释放数据库资源
            rs.close();
            stmt.close();
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex1) {
            ex1.printStackTrace();
        }



    }

}
